package com.cpkso.buss.backend.exception;

import org.apache.shiro.authz.AuthorizationException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.runze.base.web.Response;

/**
 * 全局异常处理
 * @author wrzhxy@qq.com
 * @date 2018年1月9日
 */
@ControllerAdvice
@ResponseBody
public class ExceptionHandlerAdvice {
	/**
	 * 鉴权失败
	 * @return
	 */
	@ExceptionHandler(AuthorizationException.class)
	public Response authorizationException(AuthorizationException e) {
		String msg = e.getLocalizedMessage();
		return new Response(StringUtils.hasText(msg) ? msg : "FBI WARNING", 403);
	}
	
	@ExceptionHandler(CreateEntityFailedException.class)
	public Response createEntityFailedException(CreateEntityFailedException e) {
		String msg = e.getLocalizedMessage();
		return new Response(StringUtils.hasText(msg) ? msg : "创建实体失败");
	}
	
	@ExceptionHandler(EntityHasExistException.class)
    public Response entityHasExistException(EntityHasExistException e) {
		String msg = e.getLocalizedMessage();
		return new Response(StringUtils.hasText(msg) ? msg : "Fuck you，您要操作的对象已存在");
	}
	
	@ExceptionHandler(GivenNullParamException.class)
	public Response givenNullParamException(GivenNullParamException e) {
		String msg = e.getLocalizedMessage();
		return new Response(StringUtils.hasText(msg) ? msg : "食屎啦你，没给参数");
	}
	
	@ExceptionHandler(NoSuchDataException.class)
	public Response noSuchDataException(NoSuchDataException e) {
		String msg = e.getLocalizedMessage();
		return new Response(StringUtils.hasText(msg) ? msg : "Bullshit, 数据库没数据");
	}
	
	@ExceptionHandler(DataOverflowException.class)
	public Response dataOverflowException(DataOverflowException e) {
		String msg = e.getLocalizedMessage();
		return new Response(StringUtils.hasText(msg) ? msg : "数据溢出");
	}
	
	@ExceptionHandler(UnloginException.class)
	public Response unloginException(UnloginException e) {
		String msg = e.getLocalizedMessage();
		return new Response(StringUtils.hasText(msg) ? msg : "真靠北，没登录是要怎样？", 403);
	}
	
	/**
	 * 上传异常
	 */
	@ExceptionHandler(UploadException.class)
	public Response uploadException(UploadException e) {
		String msg = e.getLocalizedMessage();
		return new Response(StringUtils.hasText(msg) ? msg : "上传失败了~", 500);
	}
}
